package com.navinfo.platform.etl.flink.protocol.entity.hy;

import com.alibaba.fastjson.JSON;
import com.navinfo.platform.etl.util.Base64Utils;
import com.navinfo.platform.etl.util.DateUtils;
import com.navinfo.platform.etl.util.LocationDataUtils;
import lombok.Data;

import java.io.UnsupportedEncodingException;

import static com.navinfo.opentsp.platform.location.protocol.common.LCDrivingBehaviorAnalysis.*;
import static com.navinfo.opentsp.platform.location.protocol.common.LCLocationData.*;
import static com.navinfo.opentsp.platform.location.protocol.common.LCTireTemperatureAddition.*;

/**
 * 通过hive表自动生成的对应Bean和Insert语句
 */
@Data
public class OdsLocation0200Pdi{
   /**
    * 自动生成的Insert语句
    * INSERT INTO ods_location_0200_pdi SELECT tid, alarm, status, longitude, latitude, originallng, originallat, height, speed, direction, gpsdate, mileage, receivedate, ispatch, oil, recorderspeed, starstatus, starnumber, signalstatus, iostatus, analogad0, analogad1, signalstrength, satellitenumber, isvalid, additionalarm, batterypower, electricvehicle, alarmfilter, standardmileage, standardfuelcon, ecudate, temperatureadditionVehtype, temperatureadditionTyreposition, temperatureadditionTyrecondition, temperatureadditionTyrepressure, temperatureadditionTyretemperature, temperatureadditionTemalarmthreshold, temperatureadditionTyrehalarmthreshold, temperatureadditionTyrelalarmthreshold, temperatureadditionTyrenomimalvalue, analysisdataTurningangle, analysisdataLowoildrivingvalue, analysisdataVehiclespeedfromecu, analysisdataRpmwhenalarming, analysisdataVelocitychangevalue, analysisdataCurrentgearshift, analysisdataCurrentrpm, analysisdataBraketimes, analysisdataClutchtimes, analysisdataRetardertimes, analysisdataAbstimes, analysisdataReversetimes, analysisdataDeviceidentity, batteryinfoMotortemperature, batteryinfoMotorcontroltemp, batteryinfoMcufaultcode, batteryinfoBmsstatus, batteryinfoBatteryavetem, batteryinfoBatterycurrent, batteryinfoTotalbatteryv, batteryinfoBatteryfaultcode, batteryinfoBatterymaxtem, batteryinfoBatterymintem, batteryinfoBatteryminsoc, batteryinfoMaxchargingv, batteryinfoMaxcharginge, batteryinfoTotalstatus, batteryinfoVehiclespeed, batteryinfoShiftstatus, batteryinfoVcufaultcode, batteryinfoEndurancemileage, batteryinfoAcceleratorpedal, batteryinfoBrakepedal, batteryinfoModelinfo, batteryinfoSwitchinfo, batteryinfoElectricattachment, batteryinfoMotorrpm, temalarmlist, alarmidentifylist, defenceadditionlist, parkingadditionlist, modulevoltageslist, staytimeparkingadditionlist, speedadditionlist, areaadditionlist, routeadditionlist, canclebreakdownadditionBreakdownlist, breakdownadditionBreakdownlist, statusaddition, createTime, staytime, serialnumber, lngtotalremaininggas, dt, hr, hashtid FROM {KAFKA_SOURCE}
    */
    private String tid;

    private String alarm;

    private String status;

    private String longitude;

    private String latitude;

    private String originalLng;

    private String originalLat;

    private String height;

    private String speed;

    private String direction;

    private String gpsDate;

    private String mileage;

    private String receiveDate;

    private String ispatch;

    private String oil;

    private String recorderSpeed;

    private String starstatus;

    private String starnumber;

    private String signalstatus;

    private String iostatus;

    private String analogad0;

    private String analogad1;

    private String signalstrength;

    private String satellitenumber;

    private String isvalid;

    private String additionalarm;

    private String batterypower;

    private String electricvehicle;

    private String alarmfilter;

    private String standardMileage;

    private String standardFuelCon;

    private String ecuDate;

    private String temperatureadditionVehtype;

    private String temperatureadditionTyreposition;

    private String temperatureadditionTyrecondition;

    private String temperatureadditionTyrepressure;

    private String temperatureadditionTyretemperature;

    private String temperatureadditionTemalarmthreshold;

    private String temperatureadditionTyrehalarmthreshold;

    private String temperatureadditionTyrelalarmthreshold;

    private String temperatureadditionTyrenomimalvalue;

    private String analysisdataTurningangle;

    private String analysisdataLowoildrivingvalue;

    private String analysisdataVehiclespeedfromecu;

    private String analysisdataRpmwhenalarming;

    private String analysisdataVelocitychangevalue;

    private String analysisdataCurrentgearshift;

    private String analysisdataCurrentrpm;

    private String analysisdataBraketimes;

    private String analysisdataClutchtimes;

    private String analysisdataRetardertimes;

    private String analysisdataAbstimes;

    private String analysisdataReversetimes;

    private String analysisdataDeviceidentity;

    private String batteryinfoMotortemperature;

    private String batteryinfoMotorcontroltemp;

    private String batteryinfoMcufaultcode;

    private String batteryinfoBmsstatus;

    private String batteryinfoBatteryavetem;

    private String batteryinfoBatterycurrent;

    private String batteryinfoTotalbatteryv;

    private String batteryinfoBatteryfaultcode;

    private String batteryinfoBatterymaxtem;

    private String batteryinfoBatterymintem;

    private String batteryinfoBatteryminsoc;

    private String batteryinfoMaxchargingv;

    private String batteryinfoMaxcharginge;

    private String batteryinfoTotalstatus;

    private String batteryinfoVehiclespeed;

    private String batteryinfoShiftstatus;

    private String batteryinfoVcufaultcode;

    private String batteryinfoEndurancemileage;

    private String batteryinfoAcceleratorpedal;

    private String batteryinfoBrakepedal;

    private String batteryinfoModelinfo;

    private String batteryinfoSwitchinfo;

    private String batteryinfoElectricattachment;

    private String batteryinfoMotorrpm;

    private String temalarmlist;

    private String alarmidentifylist;

    private String defenceadditionlist;

    private String parkingadditionlist;

    private String modulevoltageslist;

    private String staytimeparkingadditionlist;

    private String speedadditionlist;

    private String areaadditionlist;

    private String routeadditionlist;

    private String canclebreakdownadditionBreakdownlist;

    private String breakdownadditionBreakdownlist;

    private String statusaddition;

    private String createTime;

    private Long staytime;

    private Integer serialnumber;

    private Integer lngtotalremaininggas;

    private String dt;

    private String hr;

    private Integer hashtid;


    private String rowKey;

    private byte[] originalPB;

    public static OdsLocation0200Pdi of(LocationData pb, int partitionNum) throws UnsupportedEncodingException {
        OdsLocation0200Pdi bean = new OdsLocation0200Pdi();
        bean.tid = pb.getTerminalId() + "";
        bean.alarm = pb.getAlarm() + "";
        bean.status = pb.getStatus() + "";
        bean.longitude = pb.getLongitude() + "";
        bean.latitude = pb.getLatitude() + "";
        bean.originalLng = pb.getOriginalLng() + "";
        bean.originalLat = pb.getOriginalLat() + "";
        bean.height = pb.getHeight() + "";
        bean.speed = pb.getSpeed() + "";
        bean.direction = pb.getDirection() + "";
        bean.gpsDate = pb.getGpsDate() + "";
        bean.mileage = pb.getMileage() + "";
        bean.receiveDate = pb.getReceiveDate() + "";
        bean.ispatch = pb.getIsPatch() + "";
        bean.oil = pb.getOil() + "";
        bean.recorderSpeed = pb.getRecorderSpeed() + "";
        bean.starstatus = pb.getStarStatus() + "";
        bean.starnumber = pb.getStarNumber() + "";
        bean.signalstatus = pb.getSignalStatus() + "";
        bean.iostatus = pb.getIoStatus() + "";
        bean.analogad0 = pb.getAnalogAD0() + "";
        bean.analogad1 = pb.getAnalogAD1() + "";
        bean.signalstrength = pb.getSignalStrength() + "";
        bean.satellitenumber = pb.getSatelliteNumber() + "";
        bean.isvalid = pb.getIsValid() + "";
        //ByteString的特殊处理
        bean.additionalarm = JSON.toJSON(pb.getAdditionAlarm().toByteArray()).toString();
        bean.batterypower = pb.getBatteryPower() + "";
        bean.electricvehicle = pb.getElectricVehicle() + "";
        bean.alarmfilter = pb.getAlarmFilter() + "";
        bean.standardMileage = pb.getStandardMileage() + "";
        bean.standardFuelCon = pb.getStandardFuelCon() + "";
        bean.ecuDate = pb.getEcuDate() + "";

        // 对应pb中子类型展开存
        TireTemperatureAddition temAddition = pb.getTemperatureAddition();
        bean.temperatureadditionVehtype = temAddition.getVehType() + "";
        bean.temperatureadditionTyreposition = temAddition.getTyrePosition() + "";
        bean.temperatureadditionTyrecondition = temAddition.getTyreCondition() + "";
        bean.temperatureadditionTyrepressure = temAddition.getTyrePressure() + "";
        bean.temperatureadditionTyretemperature = temAddition.getTyreTemperature() + "";
        bean.temperatureadditionTemalarmthreshold = temAddition.getTemAlarmThreshold() + "";
        bean.temperatureadditionTyrehalarmthreshold = temAddition.getTyreHalarmThreshold() + "";
        bean.temperatureadditionTyrelalarmthreshold = temAddition.getTyreLalarmThreshold() + "";
        bean.temperatureadditionTyrenomimalvalue = temAddition.getTyreNomimalValue() + "";

        DrivingBehaviorAnalysis analysisData = pb.getAnalysisData();
        bean.analysisdataTurningangle = analysisData.getTurningAngle() + "";
        bean.analysisdataLowoildrivingvalue = analysisData.getLowOilDrivingValue() + "";
        bean.analysisdataVehiclespeedfromecu = analysisData.getVehicleSpeedFromEcu() + "";
        bean.analysisdataRpmwhenalarming = analysisData.getRpmWhenAlarming() + "";
        bean.analysisdataVelocitychangevalue = analysisData.getVelocityChangeValue() + "";
        bean.analysisdataCurrentgearshift = analysisData.getCurrentGearshift() + "";
        bean.analysisdataCurrentrpm = analysisData.getCurrentRPM() + "";
        bean.analysisdataBraketimes = analysisData.getBrakeTimes() + "";
        bean.analysisdataClutchtimes = analysisData.getClutchTimes() + "";
        bean.analysisdataRetardertimes = analysisData.getRetarderTimes() + "";
        bean.analysisdataAbstimes = analysisData.getAbsTimes() + "";
        bean.analysisdataReversetimes = analysisData.getReverseTimes() + "";
        bean.analysisdataDeviceidentity = Base64Utils.encode(analysisData.getDeviceIdentity().getBytes("GBK")) + "";

        BatteryVehicleInfo batteryInfo = pb.getBatteryInfo();
        bean.batteryinfoMotortemperature = batteryInfo.getMotorTemperature() + "";
        bean.batteryinfoMotorcontroltemp = batteryInfo.getMotorControlTemp() + "";
        bean.batteryinfoMcufaultcode = batteryInfo.getMcuFaultCode() + "";
        bean.batteryinfoBmsstatus = batteryInfo.getBmsStatus() + "";
        bean.batteryinfoBatteryavetem = batteryInfo.getBatteryAveTem() + "";
        bean.batteryinfoBatterycurrent = batteryInfo.getBatteryCurrent() + "";
        bean.batteryinfoTotalbatteryv = batteryInfo.getTotalBatteryV() + "";
        bean.batteryinfoBatteryfaultcode = batteryInfo.getBatteryFaultCode() + "";
        bean.batteryinfoBatterymaxtem = batteryInfo.getBatteryMaxTem() + "";
        bean.batteryinfoBatterymintem = batteryInfo.getBatteryMinTem() + "";
        bean.batteryinfoBatteryminsoc = batteryInfo.getBatteryMinSoc() + "";
        bean.batteryinfoMaxchargingv = batteryInfo.getMaxChargingV() + "";
        bean.batteryinfoMaxcharginge = batteryInfo.getMaxChargingE() + "";
        bean.batteryinfoTotalstatus = batteryInfo.getTotalStatus() + "";
        bean.batteryinfoVehiclespeed = batteryInfo.getVehicleSpeed() + "";
        bean.batteryinfoShiftstatus = batteryInfo.getShiftStatus() + "";
        bean.batteryinfoVcufaultcode = batteryInfo.getVcuFaultCode() + "";
        bean.batteryinfoEndurancemileage = batteryInfo.getEnduranceMileage() + "";
        bean.batteryinfoAcceleratorpedal = batteryInfo.getAcceleratorPedal() + "";
        bean.batteryinfoBrakepedal = batteryInfo.getBrakePedal() + "";
        bean.batteryinfoModelinfo = batteryInfo.getModelInfo() + "";
        bean.batteryinfoSwitchinfo = batteryInfo.getSwitchInfo() + "";
        bean.batteryinfoElectricattachment = batteryInfo.getElectricAttachment() + "";
        bean.batteryinfoMotorrpm = batteryInfo.getMotorRpm() + "";

        // 对应pb中repeat的数据以json方式存入
        bean.temalarmlist = JSON.toJSON(LocationDataUtils.getTemAlarmDtos(pb.getTemAlarmList())) + "";
        bean.alarmidentifylist = JSON.toJSON(pb.getAlarmIdentifyList()) + "";
        bean.defenceadditionlist = JSON.toJSON(LocationDataUtils.getKeyPointFences(pb.getDefenceAdditionList())) + "";
        bean.parkingadditionlist = JSON.toJSON(LocationDataUtils.getParkingAdditions(pb.getParkingAdditionList())) + "";
        bean.modulevoltageslist = JSON.toJSON(LocationDataUtils.getModuleVoltagess(pb.getModuleVoltagesList())) + "";
        bean.staytimeparkingadditionlist = JSON.toJSON(LocationDataUtils.getStaytimeParkingDtos(pb.getStaytimeParkingAdditionList())) + "";
        bean.speedadditionlist = JSON.toJSON(LocationDataUtils.getSpeedAdditions(pb.getSpeedAdditionList())) + "";
        bean.areaadditionlist = JSON.toJSON(LocationDataUtils.getAreaAdditions(pb.getAreaAdditionList())) + "";
        bean.routeadditionlist = JSON.toJSON(LocationDataUtils.getRouteAdditions(pb.getRouteAdditionList())) + "";
        bean.canclebreakdownadditionBreakdownlist = JSON.toJSON(LocationDataUtils.getVehicleBreakdowns(pb.getCancleBreakdownAddition().getBreakdownList())) + "";
        bean.breakdownadditionBreakdownlist = JSON.toJSON(LocationDataUtils.getVehicleBreakdowns(pb.getBreakdownAddition().getBreakdownList())) + "";
        bean.statusaddition =JSON.toJSON(LocationDataUtils.getVehicleStatusDataString(pb.getStatusAddition().getStatusList())) + "";

        bean.createTime = DateUtils.format(System.currentTimeMillis());
        bean.staytime = pb.getStayTime();
        bean.serialnumber = pb.getSerialNumber();
        bean.lngtotalremaininggas = (int)pb.getLngTotalRemainingGas();


        //添加字段只能在分割线之上添加
        //------------------------------------------分割线----------------------------------------
        bean.dt = DateUtils.format(pb.getGpsDate() * 1000, "yyyyMMdd");
        bean.hr = DateUtils.format(pb.getGpsDate() * 1000, "HH");
        bean.hashtid = (int)(pb.getTerminalId() % partitionNum);


        //rowkey:reverse(tId)|3000000000L-gpsTime
        bean.rowKey = new StringBuilder(String.valueOf(pb.getTerminalId())).reverse() +"|" + (3000000000L-pb.getGpsDate());
        bean.originalPB = pb.toByteArray();

        return bean;
    }

}

